#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string.h>
using namespace std;
typedef long long LL;
typedef long long mLL;
const LL myMax = ((1ull<<63) - 1);
vector<LL> divisor;
map<LL, LL > F;
const int MAXN = 10007;
LL prime[MAXN], p_cnt;
bool is_p[MAXN];
void Init() {
    p_cnt = 0;
    memset(is_p, 1, sizeof(is_p));
    for (int i=2; i<MAXN; ++i) {
        if (is_p[i]) {
            prime[p_cnt++] = i;
        }
        for (int j=0; j<p_cnt&&i*prime[j]<MAXN; ++j) {
            if (i%prime[j]==0) {
                is_p[i*prime[j]] = false;
                break;
            }
            is_p[i*prime[j]] = false;
        }
    }
}
inline bool check(LL div, LL b, LL n) {
    for (LL i=0; prime[i]*div<b; ++i) {
        if (n%(prime[i]*div)==0) return true;
    }
    return false;
}
LL DFS(LL b, LL n, LL st) {
    if (n<b) {
        return n;
    }
    int len = divisor.size();
    LL ret = myMax;
    for (int i=0; i<len; ++i) {
        LL div = divisor[i];
        if (n%div==0) {
            LL rd = n/div;
            if (rd%div==0 && div*div < b) continue;
            if (check(div, b, n)) continue;
            LL rt;
            if (F.count(rd)) {
                rt = F[rd];
            }
            else {
                rt = DFS(b, rd, i);
                F[rd] = rt;
            }
//            rt = DFS(b, rd, i);
            if (rt > myMax/b +b) continue;
            LL t = rt * b + div;
            if (t < 0) continue;
            if (t<ret) ret = t;
        }
    }
    return ret;
}

int main() {
#ifdef hai
    //freopen("in.txt", "r", stdin);
#endif
    Init();
    mLL b,n;
    cin>>b>>n;
    LL t = n;
    for (int i=b-1; i>1; --i) {
        while(t%i==0) {
            t = t / i;
        }
        if (n%i==0) {
            divisor.push_back(i);
        }
    }
    if (t!=1 || t>=b) printf("impossible\n");
    else {
        mLL ans = 0;
        ans = DFS(b, n, 0);
        cout<<ans<<"\n";
    }
    return 0;
}
